<?php

/**
 * Description of ParseInfo
 *
 * @author pahhan
 */
class Model_ParseInfo extends ORM
{
    protected $_table_name = 'parse_info';

    protected $_belongs_to = array('site' => array());

    public function getId()
    {
        return $this->get('id');
    }

    protected function getIdMask()
    {
        return '{id}';
    }

    public function getUrl()
    {
        return $this->url;
    }

    public function getCompleteUrl($id)
    {
        return str_replace($this->getIdMask(), $id, $this->getUrl());
    }

    public function getLastMessageId()
    {
        return $this->last_message_id;
    }

    public function getLastExternalId()
    {
        return $this->last_external_id;
    }

    public function getLastParsedPage()
    {
        return $this->get('last_parsed_page');
    }

    public function getLastUpdate()
    {
        return $this->last_update;
    }

    public function getClassKey()
    {
        return $this->class_key;
    }

    public function setLastExternalId($value)
    {
        $this->set('last_external_id', $value);
        return $this;
    }

    public function setLastUpdate($value)
    {
        $this->set('last_update', $value);
        return $this;
    }

    public function setLastMessageId($value)
    {
        $this->set('last_message_id', $value);
        return $this;
    }

    public function setLastParsedPage($value)
    {
        $this->set('last_parsed_page', $value);
        return $this;
    }

    public function getMessagesCount()
    {
        if( !$this->id ) return 0;

        $sql = 'SELECT COUNT(id) AS count FROM message WHERE parse_info_id = :id';
        $query = DB::query(Database::SELECT, $sql);
        $query->param(':id', $this->id);
        $result = $query->execute();
        return $result->get('count', 0);
    }
}

